package com.example.elderlycare.config;

import com.example.elderlycare.dao.mapper.RoleMapper;
import com.example.elderlycare.dao.mapper.RoleRouteMapper;
import com.example.elderlycare.dao.mapper.RouteMapper;
import com.example.elderlycare.dao.mapper.UserMapper;
import com.example.elderlycare.interceptor.AuthHandler;
import com.example.elderlycare.utils.CurrentUserHandlerMethodArgResolver;
import com.example.elderlycare.utils.Token;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Component
@Configuration
public class MVCConfig implements WebMvcConfigurer {
    @Resource
    UserMapper userMapper;

    @Resource
    RoleMapper roleMapper;

    @Resource
    RouteMapper routeMapper;

    @Resource
    RoleRouteMapper roleRouteMapper;

    @Resource
    Token token;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(
                        new AuthHandler(userMapper, roleMapper, routeMapper, roleRouteMapper, token))
                .addPathPatterns("/**")
                .excludePathPatterns("/login", "/screen", "/user/forgot_password/auth_code",
                        "/user/forgot_password/reset");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new CurrentUserHandlerMethodArgResolver());
    }
}